
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.ArrayList;

public class PhoneManufacturer {
    private String name;
    private int establishmentYear;

    public PhoneManufacturer(String name, int establishmentYear) {
        this.name = name;
        this.establishmentYear = establishmentYear;
    }

    public String getName() {
        return name;
    }

    public int getEstablishmentYear() {
        return establishmentYear;
    }

    public String toString() {
        return String.format("%s(%d)", name, establishmentYear);
    }
}

class ManufacturerTableModel extends AbstractTableModel {
    ArrayList<PhoneManufacturer> phoneManufacturerList = new ArrayList<>();

    public ManufacturerTableModel() {
    }

    @Override
    public int getRowCount() {
        return phoneManufacturerList.size();
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public String getColumnName(int column) {
        if (column == 0) {
            return "Name";
        } else if (column == 1) {
            return "Establishment Year";
        }
        return null;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        PhoneManufacturer phoneManufacturer = phoneManufacturerList.get(rowIndex);
        if (columnIndex == 0) {
            return phoneManufacturer.getName();
        } else {
            return phoneManufacturer.getEstablishmentYear();
        }
    }

    public void addRow(PhoneManufacturer company) {
        phoneManufacturerList.add(company);
        fireTableRowsInserted(phoneManufacturerList.size() - 1, phoneManufacturerList.size() - 1);
    }

    public void removeRow(int index) {
        if (phoneManufacturerList.size() <= index) {
            return;
        }
        phoneManufacturerList.remove(index);
        fireTableRowsDeleted(phoneManufacturerList.size(), phoneManufacturerList.size());
    }
}

class PhoneManufacturerTableDemo extends JFrame {
    JTable table;
    ManufacturerTableModel myModel;

    public PhoneManufacturerTableDemo() {
        myModel = new ManufacturerTableModel();
        table = new JTable(myModel);
        myModel.addRow(new PhoneManufacturer("Samsung", 2004));
        myModel.addRow(new PhoneManufacturer("Apple", 2007));

        JPanel input_panel = new JPanel();

        getContentPane().add(input_panel, BorderLayout.SOUTH);
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    //ADD ManufacturerTableModel class
    //Make appropriate changes to the code for testing your new table model.
    //One way to check this would be to make sure it displays PhoneManufacturer contents on the JTable view.
    //Fix any compile time errors in this file in the process.

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new PhoneManufacturerTableDemo();
            }
        });

//        ManufacturerTableModel data = new ManufacturerTableModel();
//        data.addRow(new PhoneManufacturer("Samsung", 2004));
//        data.addRow(new PhoneManufacturer("Apple", 2007));
//        System.out.println(data.getValueAt(0, 0));
//        Object first =  data.getValueAt(0, 0);
//        if (!(first instanceof String)) System.out.println("The first column must be a String");

//        ManufacturerTableModel data = new ManufacturerTableModel();
//        data.addRow(new PhoneManufacturer("Samsung", 2004));
//        data.addRow(new PhoneManufacturer("Apple", 2007));
//        System.out.println(data.getValueAt(1, 0));
//        System.out.println(data.getValueAt(1, 1));
    }
}